package com.example.emb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.emb.entity.Classdstutable;
import com.example.emb.mapper.ClassdStutableMapper;
import com.example.emb.mapper.ClassdtableMapper;
import com.example.emb.service.TeacherImpStuService;
import com.example.emb.service.exception.UsrNotFoundException;
import com.example.emb.until.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import org.checkerframework.checker.units.qual.K;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "教师关于学生管理的操作的api所在")
@RequestMapping("/zjgsEmb/TeacherManageStu")
public class TeacherStuController {
    @Autowired
    TeacherImpStuService teacherImpStuService;
    @Autowired
    ClassdtableMapper classdtableMapper;
    @Autowired
    ClassdStutableMapper classdStuMapper;

    @ApiOperation("获得班级下的所有学生信息")
    @GetMapping("/getStuByClass")
    private JsonResult<List<Classdstutable>> getStuByClass(int classId){
        List<Classdstutable> list=new ArrayList<>();
        QueryWrapper<Classdstutable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("classId",classId);
        list=classdStuMapper.selectList(queryWrapper);
        return new JsonResult<List<Classdstutable>>(200,list);
    }

    @ApiOperation("通过excel批量导入班级中的学生info")
    @PostMapping("ImportStu")
    private JsonResult<String> ImportStu(@RequestParam("file") MultipartFile file,int classId,int classType){
        System.out.println("begin to load");
        System.out.println(file.getContentType());
        if (!file.getContentType().equals("application/vnd.ms-excel") && !file.getContentType().equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            return new JsonResult(10001,"上传非excel文件");
        }
        //如果操作失败了，应该要有这里有自动回滚和手动回滚的方法
        try {
            teacherImpStuService.ImportStuByExcel(file, classType,classId);
            return new JsonResult(200);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(500, "服务器内部错误: " + e.getMessage());
        }
    }


    @ApiOperation("手动新建一个学生")
    @PostMapping("InsertStu")
    private JsonResult<String> InsertStu(String uid,int classId,String TeachingClassName){
        try {
            teacherImpStuService.InsertStu(uid,classId,TeachingClassName);
        }catch (UsrNotFoundException e){
            return  new JsonResult<String>(505,"该学生不存在，请使用excel先导入学生");
        }
        return new JsonResult(200);
    }

    @ApiOperation("手动删除班级中的学生info")
    @PostMapping("DeleteStu")
    private JsonResult DeleteStu(int classStuId){
        teacherImpStuService.DeleteStu(classStuId);
        return new JsonResult(200);
    }

}
